package com.koro.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.fasterxml.jackson.core.type.TypeReference;
import com.koro.dto.FindUsersDTO;
import com.koro.entity.Users;
import com.koro.mapper.UsersMapper;
import com.koro.service.UsersService;
import com.koro.tool.ObjectMapperUtil;
import com.koro.vo.BaseException;
import com.koro.vo.FindUsersVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;

import java.util.List;

@Service
public class UsersServiceImpl implements UsersService {

    @Autowired
    private UsersMapper usersMapper;

    @Override
    public FindUsersVO findById(FindUsersDTO dto) {
        QueryWrapper<Users> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("id",dto.getId());
        queryWrapper.eq("type",1);
        Users users = usersMapper.selectOne(queryWrapper);
        if(ObjectUtils.isEmpty(users)) {
            throw new BaseException("用户不存在，请核查.");
        }
        FindUsersVO vo = new FindUsersVO();
        BeanUtils.copyProperties(users,vo);
        return vo;
    }

    @Override
    public List<FindUsersVO> findAll(Integer type) {
        QueryWrapper<Users> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("type",type);
        List<Users> list = usersMapper.selectList(queryWrapper);
        return ObjectMapperUtil.convert(list, new TypeReference<List<FindUsersVO>>() {
        });
    }
}
